<?php
    require_once LIBPATH.'spyc'.EXT;

    class Configurator {
        private $configs = array();
        private $configDir = 'config';

        public function __construct() {
            $this->init();
            $GLOBALS['CONFIGURATOR'] = $this;
        }
        
        public static function getConfiguration($config) {
            $configurator = $GLOBALS['CONFIGURATOR'];
            return $configurator->getConfig($config);
        }

        public function getConfig($name) {
            return $this->configs[$name];
        }
        
        private function loadEnvironmentConfig($file) {
            $config = Spyc::YAMLLoad($this->getConfigDir().$file);
            return $config[ENVIRONMENT];
        }
        
        private function init() {
            $this->loadConfigDir($this->getConfigDir());
        }
        
        private function getConfigDir() {
            return dirname(__FILE__).DIRECTORY_SEPARATOR.$this->configDir.DIRECTORY_SEPARATOR;
        }
        
        private function loadConfigDir($dir) {
            foreach(scandir($dir) as $file) {
                if (preg_match('/^.+\.yml$/', $file)) {
                    $config = substr($file, 0, strlen($file) - 4);
                    $this->configs[$config] = $this->loadEnvironmentConfig($file);
                }
            }
        }
    }
    
    new Configurator();
?>
